// 学生
const router = require('express').Router()
const db = require('../db')

// 批量插入学生数据
router.post('/add', (req, res) => {
    let { user } = req.query
    if (user.role == 'student') return;
    let sql = ''
    let values = req.body
    sql = 'INSERT INTO student (id, name, sex, password, classId) VALUES ?'
    if (!values[0][0]) {
        db.query(`SELECT max(id) FROM student `, {}, result => {
            let max = result[0]['max(id)'] + 1;
            for (let i = 0; i < values.length; i++) {
                values[i][0] = ++max
            }
            db.query(sql, [values], result => {
                res.json({
                    code: 0,
                    msg: '添加学生成功'
                })
            })
        })
    } else {
        db.query(sql, [values], result => {
            res.json({
                code: 0,
                msg: '添加学生成功'
            })
        })
    }

})

// 删除学生 可以批量
router.post('/del', (req, res) => {
    let { user } = req.query
    if (user.role == 'student') return;
    let sql = 'DELETE FROM student WHERE id IN (?)'
    console.log('req.body', req.body)
    db.query(sql, [req.body], result => {
        console.log('result', result)
        res.json({
            code: 0,
            type: 'success',
            msg: '删除成功'
        })
    })
})


router.get('/search', (req, res) => {
    let { page, limit, query } = req.query
    let where = ''
    console.log('query', query)
    if (!query) {
        res.json({ code: 2, msg: '没有查询参数' })
    }
    if (parseInt(query)) {
        // sql = `SELECT * FROM student_view WHERE studentId like '%${query}%' `
        where = `WHERE studentId like '%${query}%' `
    } else {
        where = `WHERE studentName like '%${query}%'`
        // sql = `SELECT * FROM student_view WHERE studentName like '%${query}%'`
    }
    db.query(` SELECT count(*) FROM student_view  ${where}`, {}, result => {
        let totalCount = result[0]['count(*)'];
        db.query(` SELECT * FROM student_view  ${where} LIMIT ${(page - 1) * limit}, ${limit}`, {}, list => {
            res.json({
                code: 0,
                page: {
                    currPage: page,
                    pageSize: limit,
                    list,
                    totalCount,
                    totalPage: Math.ceil(totalCount / limit)
                }
            })
        })
    })
})


// db.query(`SELECT count(*) FROM student_view  WHERE  classId = ${classId} `, {}, result => {
//     let totalCount = result[0]['count(*)'];
//     db.query(`SELECT studentId, studentName, sex, classId, className FROM student_view WHERE classId = ${classId}  LIMIT ${(page - 1) * limit}, ${limit} `, {}, list => {

//         res.json({
//             code: 0,
//             page: {
//                 currPage: page,
//                 pageSize: limit,
//                 list,
//                 totalCount,
//                 totalPage: Math.ceil(totalCount / limit)
//             }
//         })
//     })
// })
// 得到一个班级的所有学生
router.get('/getStudentList', (req, res) => {
    console.log('0000000000000000')
    if (req.query.classId && req.query.role !== 'student') {
        db.query('SELECT id, name, sex, classId FROM student WHERE classId = ?;', req.query.classId, result => {
            res.json(result)
        })

    }
})



module.exports = router